package com.inifitness.tenant.starter;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.inifitness.common.constants.GlobalConstants;

import lombok.extern.log4j.Log4j2;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 程序启动信息提示
 *
 * @author sunjinfeng
 * @version 1.0
 * @since 2023/4/26 15:33
 */
@Configuration
@Log4j2
public class ApplicationStarter {

  @Value("${server.servlet.context-path}")
  private String contextPath;
  @Value("${server.port}")
  private String port;

  @Bean
  public ApplicationRunner applicationRunner() {
    return applicationArguments -> {
      log.debug(()-> MessageFormatter.format("启动成功：{}", getApplicationUrl()));
      log.info(()-> MessageFormatter.format("OpenAPI：{}", getApplicationUrl("/swagger-ui/index.html")));
      log.info(()->MessageFormatter.format("KnifeAPI：{}" , getApplicationUrl("/doc.html")));
    };
  }
  private String getApplicationUrl(){
    return getApplicationUrl("");
  }

  private String getApplicationUrl(String uri){
    try {
      return String.format("%s://%s:%d%s%s", GlobalConstants.HTTP,InetAddress.getLocalHost()
          .getHostAddress(),port,contextPath,uri);
    }catch (UnknownHostException e) {
      //输出到日志文件中
      log.error("获取LocalHost时出现异常", e);
    }
    return "";
  }
}
